package com.prs.crm.action.official;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.official.PersonalFile;
import com.prs.crm.domain.sys.FileTypeMaintain;
import com.prs.crm.service.official.PersonalFileService;
import com.prs.crm.service.sys.FileTypeMaintainService;

public class PersonalFileAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private PaginationSupport pagination;
	private PersonalFile personalFile;
	private PersonalFileService personalFileService;
	private File upload;
	private String uploadFileName;
	private Collection<TreeNode> nodes;
	private FileTypeMaintainService fileTypeMaintainService;
	public FileTypeMaintainService getFileTypeMaintainService() {
		return fileTypeMaintainService;
	}
	public void setFileTypeMaintainService(
			FileTypeMaintainService fileTypeMaintainService) {
		this.fileTypeMaintainService = fileTypeMaintainService;
	}
	public Collection<TreeNode> getNodes() {
		return nodes;
	}
	public void setNodes(Collection<TreeNode> nodes) {
		this.nodes = nodes;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public PersonalFile getPersonalFile() {
		return personalFile;
	}
	public void setPersonalFile(PersonalFile personalFile) {
		this.personalFile = personalFile;
	}
	public PersonalFileService getPersonalFileService() {
		return personalFileService;
	}
	public void setPersonalFileService(PersonalFileService personalFileService) {
		this.personalFileService = personalFileService;
	}
	
	@Action("listPersonalFile")
	public String listPersonalFile(){
		Person currorPerson=AppContext.getAccountContext().currentUser();
		List<PersonalFile> pf=this.getPersonalFileService().getAllPersonalFile();
		for (PersonalFile pfs : pf) {
			if (pfs.getRecorder().getId().equals(currorPerson.getId())) {
				pfs.setViState(1);
				pfs.setUpState(1);
				this.getPersonalFileService().submit(pfs);
			}else{
				if (pfs.getVisit()==null || "".equals(pfs.getVisit().trim())) {
					pfs.setViState(1);//表示有查看权限
					this.getPersonalFileService().submit(pfs);
				}else{
					String[] pers=pfs.getVisit().split(",");
					for (String perss : pers) {
						if (currorPerson.getResume().getName().equals(perss)) {
							pfs.setViState(1);
							this.getPersonalFileService().submit(pfs);
							break;
						}else{
							pfs.setViState(2);//没有查看权限 
							this.getPersonalFileService().submit(pfs);
						}
					}
				}
				if  (pfs.getControl()==null || "".equals(pfs.getControl().trim())) {
					pfs.setUpState(1);//表示有修改权限
					this.getPersonalFileService().submit(pfs);
				}else{
					String[] upers=pfs.getControl().split(",");
					for (String uperss : upers) {
						if (currorPerson.getResume().getName().equals(uperss)) {
							pfs.setUpState(1);
							this.getPersonalFileService().submit(pfs);
							break;
						}else{
							pfs.setUpState(2);//没有查看权限 
							this.getPersonalFileService().submit(pfs);
						}
					}
				}
			}
			if ("文件夹".equals(pfs.getType())) {
				pfs.setVistates(1);
				this.getPersonalFileService().submit(pfs);
			}else{
				if (pfs.getRecorder().getId().equals(currorPerson.getId())) {
					pfs.setVistates(1);
					this.getPersonalFileService().submit(pfs);
				}else{
					pfs.setVistates(2);
					this.getPersonalFileService().submit(pfs);
				}
			}
		}
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(PersonalFile.class)
		.add(Restrictions.isNull("parent"));
		criteria.add(Restrictions.eq("viState", 1));
		criteria.add(Restrictions.eq("vistates", 1));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPersonalFileService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action("listPersonalFiles")
	public String listPersonalFiles(){
		Person currorPerson=AppContext.getAccountContext().currentUser();
		List<PersonalFile> pf=this.getPersonalFileService().getAllPersonalFile();
		for (PersonalFile pfs : pf) {
			if (pfs.getRecorder().getId().equals(currorPerson.getId())) {
				pfs.setViState(1);
				pfs.setUpState(1);
				this.getPersonalFileService().submit(pfs);
			}else{
				if (pfs.getVisit()==null || "".equals(pfs.getVisit().trim())) {
					pfs.setViState(1);//表示有查看权限
					this.getPersonalFileService().submit(pfs);
				}else{
					String[] pers=pfs.getVisit().split(",");
					for (String perss : pers) {
						if (currorPerson.getResume().getName().equals(perss)) {
							pfs.setViState(1);
							this.getPersonalFileService().submit(pfs);
							break;
						}else{
							pfs.setViState(2);//没有查看权限 
							this.getPersonalFileService().submit(pfs);
						}
					}
				}
				if  (pfs.getControl()==null || "".equals(pfs.getControl().trim())) {
					pfs.setUpState(1);//表示有修改权限
					this.getPersonalFileService().submit(pfs);
				}else{
					String[] upers=pfs.getControl().split(",");
					for (String uperss : upers) {
						if (currorPerson.getResume().getName().equals(uperss)) {
							pfs.setUpState(1);
							this.getPersonalFileService().submit(pfs);
							break;
						}else{
							pfs.setUpState(2);//没有查看权限 
							this.getPersonalFileService().submit(pfs);
						}
					}
				}
			}
			if ("文件夹".equals(pfs.getType())) {
				pfs.setVistates(1);
				this.getPersonalFileService().submit(pfs);
			}else{
				if (pfs.getRecorder().getId().equals(currorPerson.getId())) {
					pfs.setVistates(1);
					this.getPersonalFileService().submit(pfs);
				}else{
					pfs.setVistates(2);
					this.getPersonalFileService().submit(pfs);
				}
			}
		}
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(PersonalFile.class);
		criteria.add(Restrictions.eq("parent",this.getPersonalFileService().getPersonalFileById(personalFile.getId())));
		criteria.add(Restrictions.eq("viState", 1));
		criteria.add(Restrictions.eq("vistates", 1));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPersonalFileService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action("editFolder")
	public String editFolder(){
		return SUCCESS;
	}
	@Action("editFile")
	public String editFile(){
		return SUCCESS;
	}
	
	@Action("editUploadFile")
	public String editUploadFile(){
		return SUCCESS;
	}
	
	@Action("editRename")
	public String editRename(){
		return SUCCESS;
	}
	@Action("editSerach")
	public String editSerach(){
		return SUCCESS;
	}
	
	@Action("editMove")
	public String editMove(){
		return SUCCESS;
	}
	@Action(value = "saveMoveAndRename", results = {
			@Result(name = "success", type = "redirect", location = "listPersonalFile"),
			@Result(name = "error", type = "chain", location = "editFolder") })
	public String saveMoveAndRename(){
		Person person=AppContext.getAccountContext().currentUser();
		personalFile.setLastPerson(person.getResume().getName());
		personalFile.setLastTime(new Date());
		this.getPersonalFileService().submit(personalFile);
		return SUCCESS;
	}
	@Action(value = "saveFolder", results = {
			@Result(name = "success", type = "redirect", location = "listPersonalFile"),
			@Result(name = "error", type = "chain", location = "editFolder") })
	public String saveFolder(){
		if (personalFile.getName()==null || "".equals(personalFile.getName().trim())) {
			this.addActionError("文件名字不能为空");
			return ERROR;
		}
		personalFile.setType("文件夹");
		personalFile.setState(1);
		Person person=AppContext.getAccountContext().currentUser();
		personalFile.setLastPerson(person.getResume().getName());
		personalFile.setLastTime(new Date());
		if (personalFile.getId()==null) {
			this.getPersonalFileService().save(personalFile);
		}else{
			this.getPersonalFileService().submit(personalFile);
		}
		return SUCCESS;
	}
	@Action(value = "saveFile", results = {
			@Result(name = "success", type = "redirect", location = "listPersonalFile"),
			@Result(name = "error", type = "chain", location = "editFile") })
	public String saveFile(){
		personalFile.setState(2);
		Person person=AppContext.getAccountContext().currentUser();
		personalFile.setLastPerson(person.getResume().getName());
		personalFile.setLastTime(new Date());
		if (personalFile.getId()==null) {
			this.getPersonalFileService().save(personalFile);
		}else{
			this.getPersonalFileService().submit(personalFile);
		}
		return SUCCESS;
	}
	@Action(value = "saveUploadFile", results = {
			@Result(name = "success", type = "redirect", location = "listPersonalFile"),
			@Result(name = "error", type = "chain", location = "editUploadFile") })
	public String saveUploadFile(){
		if (upload!=null) {
			this.personalFile.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
			List<FileTypeMaintain> ft=this.getFileTypeMaintainService().getFileTypeMaintain(personalFile.getAttach().getContentType());
			for (FileTypeMaintain fileTypeMaintain : ft) {
				personalFile.setFileTypeMaintain(fileTypeMaintain);
			}
		}else{
			this.addActionError("请上传文件！");
			return ERROR;
		}
		personalFile.setName(personalFile.getAttach().getName());
		personalFile.setType("other");
		personalFile.setState(3);
		Person person=AppContext.getAccountContext().currentUser();
		personalFile.setLastPerson(person.getResume().getName());
		personalFile.setLastTime(new Date());
		if (personalFile.getId()==null) {
			this.getPersonalFileService().save(personalFile);
		}else{
			this.getPersonalFileService().submit(personalFile);
		}
		return SUCCESS;
	}
	@Action(value = "loadPersonalFileTree", results = { @Result(name = "success", type = "json", params = {
			"root", "nodes" }) })
	public String loadPersonalFileTree(){
		nodes = new ArrayList<TreeNode>();
		Collection<PersonalFile> pf=this.getPersonalFileService().getRootPersonalFile();
		for (PersonalFile personalFile : pf) {
			TreeNode node = new TreeNode();
			node.setId(String.valueOf(personalFile.getId()));
			node.setText(personalFile.getName());
			addChildNode(personalFile, node);
			nodes.add(node);
		}
		return SUCCESS;
	} 
	private void addChildNode(PersonalFile personalFile, TreeNode parentnode) {
		if (personalFile.getChilds()!=null) {
			for (PersonalFile personalFiles : personalFile.getChilds()) {
				if ("文件夹".equals(personalFiles.getType().trim())) {
					TreeNode node = new TreeNode();
					node.setId(String.valueOf(personalFiles.getId()));
					node.setText(personalFiles.getName());
					parentnode.addChildNode(node);
					addChildNode(personalFiles, node);
				}
			}
		}
	}
}
